<template>
  <el-container>
    <el-aside width="210px">
      <el-menu
          :default-openeds="['1']"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          style="height: 100vh;"
      >
        <el-submenu index="1">
          <template slot="title">
            <i class="el-icon-setting"></i>
            <span>系统管理</span>
          </template>
          <el-menu-item-group>
            <el-menu-item index="1-1" >
              <i class="el-icon-user"></i>
              <router-link to="/admin/user" tag="span">用户管理</router-link>
            </el-menu-item>
            <el-menu-item index="1-2">
              <i class="el-icon-s-custom"></i>
              <router-link to="/admin/role" tag="span">角色管理</router-link>
            </el-menu-item>
          </el-menu-item-group>
        </el-submenu>

      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        当前用户:{{ getUsername }}
        <el-button type="danger" size="mini" @click="logout">退出</el-button>
      </el-header>
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import axios from "axios";

export default {
  name: "admin",
  data() {
    return {}
  },
  computed: {
    getUsername(){
      return this.$store.state.username
    }
  },
  methods: {
    logout(){
      // 后端退出登录
      axios.post("/user/logout",{
        token: this.$store.state.token
      }).then(res => {
        if (res.code == 2000) {
          this.$message.success("退出成功");
          // 清空 Vuex 中的用户名
          this.$store.commit('setUsername', '');
          this.$store.commit('setToken', '');
          // 退出成功后,跳转到登录页
          this.$router.push("/");
        }
      }).catch(err => {
        console.log(err)
      })
    },
  }
}
</script>

<style scoped>

.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: right;
  line-height: 60px;
}

.el-aside {
  background-color: #545c64;
  color: #333;
  text-align: left;
  line-height: 100vh;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: left;
}


</style>